package com.example.livemall.service.impl;


import com.example.livemall.dto.LzjResult;
import com.example.livemall.entity.Banner;
import com.example.livemall.mapper.BannerMapper;
import com.example.livemall.service.BannerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * Banner 服务业务层
 */
@Service
public class BannerServiceImpl implements BannerService {

    // 注入 Banner 服务数据层
    @Autowired
    private BannerMapper bannerMapper;

    @Value("${origin}")
    private String origin;

    /**
     * 查询 Banner 集合
     *
     * @return
     */
    @Override
    public LzjResult selectBannerList() {
        List<Banner> banners= bannerMapper.getBannerList();
        if (CollectionUtils.isEmpty(banners)) {
            //返回失败对象
			return LzjResult.error("查询失败");
		}
        // 处理图片地址
        List<String> imgUrls =
				banners.stream().map((item) -> origin + item.getUrl()).collect(Collectors.toList());
        // 返回成功对象
        return LzjResult.success("查询成功", imgUrls);
    }

}